/*
 * Created by JFormDesigner on Sun Jun 06 20:44:06 CST 2021
 */

package com.J1ay.carsys.gui.Emp.Dialog;

import java.awt.event.*;
import com.J1ay.carsys.gui.tishi.Success;
import com.J1ay.carsys.gui.tishi.Warning;
import com.J1ay.carsys.pojo.*;
import com.J1ay.carsys.service.EmpService;
import com.J1ay.carsys.service.Impl.EmpServiceImpl;
import com.J1ay.carsys.service.Impl.UserServiceImpl;
import com.J1ay.carsys.service.UserService;

import java.awt.*;
import javax.swing.*;
import javax.swing.GroupLayout;

/**
 * @author Brainrain
 */
public class Del1 extends JDialog {
    EmpService empService = new EmpServiceImpl();
    UserService userService = new UserServiceImpl();
    String type;
    Bower Delbower;
    Car Delcar;
    //    删除租车人
    public Del1(Window owner, Bower delBower) {
        super(owner);
        initComponents();
        Delbower = delBower;
        this.label2.setText(delBower.getBname() + ",电话为:"+delBower.getPhone());
        type = "bower";
    }
    //    删除车辆
    public Del1(Window owner, Car delcar) {
        super(owner);
        initComponents();
        Delcar = delcar;
        type = "car";
        this.label2.setText(delcar.getName() + ",车牌号为:" + delcar.getNum());
    }
    public void delBower(String id,String phone){
        Result result = empService.deleteBower(id,phone);
        if (200 == result.getStatus()){
            new Success(this,"审核不通过成功!").start();
            this.dispose();
        }
        else{
            new Warning(this,result.getMsg()).start();
        }
    }
    public void delCar(int id, String num){
        Result result =  empService.deleteCar(id,num);
        if (200 == result.getStatus()){
            new Success(this,"删除成功!").start();
            this.dispose();
        }
        else{
            new Warning(this,result.getMsg()).start();
        }
    }
    public void start() {
        this.setVisible(true);
    }

    private void button1ActionPerformed(ActionEvent e) {
        // TODO add your code here
        if(type.equals("car")) {
            if (Delcar.getStatus().equals("yes")) {
                new Warning(this,"该车辆正在被租借,请换一辆").start();
                this.dispose();
            }
            else {
                this.delCar(Delcar.getId(),Delcar.getNum());
            }
        }
        else {
            BowCar[] bowCars = (BowCar[]) userService.getCarByUId(Delbower.getUId()).getData();
            if(bowCars.length > 0) {
                new Warning(this,"该会员还有车未还，请换一人").start();
                this.dispose();
            }
            else {
                this.delBower(Delbower.getUId(),Delbower.getPhone());
            }
        }

    }

    private void button2ActionPerformed(ActionEvent e) {
        // TODO add your code here
        this.dispose();
    }
    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label1 = new JLabel();
        button1 = new JButton();
        button2 = new JButton();
        label2 = new JLabel();

        //======== this ========
        setTitle("\u5220\u9664");
        Container contentPane = getContentPane();

        //---- label1 ----
        label1.setText("\u786e\u5b9a\u5ba1\u6838\u4e0d\u901a\u8fc7\u5417\uff1f");

        //---- button1 ----
        button1.setText("\u786e\u5b9a");
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                button1ActionPerformed(e);
            }
        });

        //---- button2 ----
        button2.setText("\u53d6\u6d88");
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                button2ActionPerformed(e);
            }
        });

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(43, 43, 43)
                    .addComponent(button1)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 133, Short.MAX_VALUE)
                    .addComponent(button2)
                    .addGap(42, 42, 42))
                .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                    .addContainerGap(93, Short.MAX_VALUE)
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                            .addComponent(label1, GroupLayout.PREFERRED_SIZE, 187, GroupLayout.PREFERRED_SIZE)
                            .addGap(56, 56, 56))
                        .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                            .addComponent(label2, GroupLayout.PREFERRED_SIZE, 259, GroupLayout.PREFERRED_SIZE)
                            .addContainerGap())))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(22, 22, 22)
                    .addComponent(label1, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
                    .addComponent(label2, GroupLayout.PREFERRED_SIZE, 29, GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(button2)
                        .addComponent(button1))
                    .addGap(24, 24, 24))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label1;
    private JButton button1;
    private JButton button2;
    private JLabel label2;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
